package edu.pku.sei.mte.mtemodel.mtmodel.expression;

import java.util.List;

import edu.pku.sei.mte.mtemodel.mtmodel.rulebased.Rule;
import edu.pku.sei.mte.runtime.Context;
import edu.pku.sei.mte.runtime.rulebased.RBTContext;
import edu.pku.sei.mte.utilities.Visitor;

public class CallRelationExpression extends CallRuleExpression {
	@SuppressWarnings("unchecked")
	public Object evaluate(Context context, boolean enforce) {
		RBTContext pc = (RBTContext)context;

		Rule rule = pc.getTransformation().getRule(ruleName);
		if(rule==null){
			return Expression.ERROR_VALUE;
		}
		RBTContext cc = pc.getChildRuleContext();
		cc.setRule(rule);
		rule.registVariableToContext(cc);
		
		setup(pc,cc,enforce);
		
/*		if(enforce){
			boolean  flag = rule.enforce(cc);
			return flag?Boolean.TRUE:Boolean.FALSE;
		} else {
			List<Context> ccs = (List<Context>) rule.checkOnly(cc, false, false);
			
			if(ccs==null){
				return Boolean.TRUE;
			} else if(ccs.size()==0)
				return Boolean.FALSE;
			else if(ccs.size()>1){
				System.out.println("more satisfied context in relation call expression");
			}
			cc = (RBTContext)ccs.get(0);
		}
*/		
		setdown(pc,cc,enforce);
		
		return Boolean.TRUE;
	}

	public Object evaluate(Context context, boolean enforce,
			Object expectedValue) {
		
		return null;
	}

	@Override
	public Object accept(Visitor visitor) throws Exception {
		return visitor.visitCallRelationExpression(this);
	}
}
